package com.example.busquerysystem.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.busquerysystem.MyApplication;
import com.example.busquerysystem.R;
import com.example.busquerysystem.activity.MapActivity;
import com.example.busquerysystem.bean.BusStation;
import com.example.busquerysystem.bean.User;
import com.example.busquerysystem.inter.AddInterface;
import com.example.busquerysystem.inter.LoginInterface;
import com.example.busquerysystem.inter.Station;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/11 0011.
 */

public class HttpUtil {
    public static void Login(String editInfo, String editPwd, final Context context, final Activity activity) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://114.67.157.123/servlet/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        LoginInterface service = retrofit.create(LoginInterface.class);//获取API接口的实现类的实例对象
        Call<User> gsonnn = service.getResult(editInfo, editPwd);
        gsonnn.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User gsont = response.body();
                String aaa = gsont.getLogin();
                if (aaa.equals("200")) {
//                    Toast.makeText(context, gsont.getLogin(), Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, MapActivity.class));
                    activity.finish();
                } else {
                    Toast.makeText(context, "账号或密码错误:" + gsont.getLogin(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
//                Toast.makeText(Activity_menu_login.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                Toast.makeText(context, "请检查网络连接", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public static void AddUser(final String user, final String pwd, final Activity context) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://114.67.157.123/servlet/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AddInterface service = retrofit.create(AddInterface.class);//获取API接口的实现类的实例对象
        Call<User> gsonnn = service.getResult1(user, pwd);
        gsonnn.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User aaa = response.body();
                if (aaa.getInsert().equals("200")) {
                    new AlertDialog.Builder(context)
                            .setTitle("欢迎使用")
                            .setMessage("您已成功注册账号")
                            .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    context.finish();
                                    context.overridePendingTransition(R.anim.out_to_left,
                                            R.anim.in_to_right);
                                }
                            }).show();
                } else {
                    Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(context, "no", Toast.LENGTH_SHORT).show();
            }
        });
    }


}